﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class GameOver : MonoBehaviour {

    EginXTJ xtj = EginXTJ.Instance;
    public List<GameObject> ScoreSprites;
    public List<GameObject> ScoreFathers;
    public List<UISprite> JiaJians;
    public PaiDealCardBroadcast newpb;
    public List<XTJUser> xtjUsers;
    public GameObject gameOverPanel;
    public AudioClip victoryClip;//胜利音效
    public AudioClip defeatClip;//失败音效

    private GameObject cardsPanel;
    private XTJGame xtjGame;
    private XTJFriendGame xtjFriendGame;
    void Awake()
    {
        if(xtj.roundType == 1){
            cardsPanel = GameObject.Find("UI Root/CardsPanel");
            xtjGame = GameObject.Find("UI Root/XTJGame").GetComponent<XTJGame>();
            for (int i = 0; i < 4; i++)
            {
                xtjUsers[i] = GameObject.Find("UI Root/XTJGame/Players/Player" + i).GetComponent<XTJUser>();
            }
        }
        else if (xtj.roundType == 3)
        {
            xtjFriendGame = GameObject.Find("UI Root/XTJGamePanel").GetComponent<XTJFriendGame>();
            for (int i = 0; i < 4; i++)
            {
                xtjUsers[i] = GameObject.Find("UI Root/XTJGamePanel/Players/Player" + i).GetComponent<XTJUser>();
            }
        }
       
       
       
    }
    void Start()
    {
        if(newpb != null){
            //for (int i = 0; i < newpb.CoinMap.Count; i++)
            {
                foreach (KeyValuePair<int, PaiInt64Array> keyValue in newpb.CoinMap)
                {
                    //if(xtjUsers[i].uid == keyValue.Key)
                    {
                        //求得玩家在房间中对应的位置
                        int tempIndex = xtj.ReturnPlayerSeat(keyValue.Key );
                        Debug.Log("游戏结束分数：" + keyValue.Key + "  " + tempIndex + " " + keyValue.Value.Value[1]);
                        //显示输赢的金币数
                        ShowScore(keyValue.Value.Value[0], ScoreSprites[tempIndex], ScoreFathers[tempIndex], JiaJians[tempIndex]);
                        //显示剩余的金币数
                        xtjUsers[tempIndex].goldLabel.text = EginUser.Instance.ReturnMoneyForLong(keyValue.Value.Value[1]);
                    }
                    if (int.Parse(EginUser.Instance.Uid) == keyValue.Key)
                    {
                        EginUser.Instance.coinCount = keyValue.Value.Value[1].ToString();
                        if (keyValue.Value.Value[0] >= 0)
                        {
                            //播放胜利的音效
                            SoundModel.PlaySound(victoryClip);
                        }
                        else
                        {
                            //播放失败的音效
                            SoundModel.PlaySound(defeatClip);
                        }
                    }
                }
            }      
        }
            
        StartCoroutine(DestroyGameObect());
    }

    /// <summary>
    /// 显示加减的分数
    /// </summary>
    public void ShowScore(long score, GameObject obj1, GameObject obj1Father, UISprite sprite)
    {
        string plusLess = "";
        float x = obj1.transform.localPosition.x;
        float y = obj1.transform.localPosition.y;        
       
        string tempStr = "";
        
        //分数大于0
        if (score >= 0)
        {
            plusLess = "plus_";
            //显示+
            sprite.gameObject.SetActive(true);
            sprite.spriteName = "plus_" + "jia";
            tempStr = EginUser.Instance.ReturnMoneyForLong(score);
        }
        else
        {
            plusLess = "less_";
            //显示-
            sprite.gameObject.SetActive(true);
            sprite.spriteName = "less_" + "jian";
            tempStr = EginUser.Instance.ReturnMoneyForLong(long.Parse(score.ToString().Remove(0, 1)));
        }

        int len = tempStr.Length;

        //设置分数
        for (int i = 0; i < len; i++)
        {
            GameObject obj = Instantiate(obj1);
            obj.transform.parent = obj1Father.transform;
            obj.transform.localScale = Vector3.one;
            obj.SetActive(true);
            string str = tempStr.Substring(0, 1);
            //Debug.Log(str);
            if (i == 0)
            {
                obj.transform.localPosition = new Vector3(x, y, 0);
            }
            else
            {
                if (str == ".")
                {
                    x += 26;

                }
                else if (str == "万")
                {
                    x += 50;
                    str = "w";
                }
                else if (str == "亿")
                {
                    x += 50;
                    str = "yi";
                }
                else
                {
                    x += 40;
                }
                obj.transform.localPosition = new Vector3(x, y, 0);
            }
            tempStr = tempStr.Remove(0, 1);
            obj.GetComponent<UISprite>().spriteName = plusLess + str;

        }
    }


    IEnumerator DestroyGameObect()
    {        
        yield return new WaitForSeconds(5f);
        if(xtjGame != null){
            xtjGame.RecoverInit();
        }
               
        if(cardsPanel != null){
            Destroy(cardsPanel);
        }

        if(xtjFriendGame != null){
            xtjFriendGame.BackOriginal();
            xtjFriendGame.RecoverInit();
        }
        Destroy(gameOverPanel);
    }
}
